Imports System.Xml.Serialization
Imports System.Runtime.Serialization
Imports System.Xml
Imports System.Xml.Schema

Public Class Edmx
    Inherits XDocument

    ''' <summary>Initializes an instance of the <see cref="Edmx" /> class.
    ''' This is the default constructor for this class.</summary>
    Public Sub New()
        MyBase.New(New XDeclaration("1.0", "UTF-8", "yes"), <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"/>)
    End Sub

    Public Sub LoadFile(ByVal fileName As String)

        Using reader As New IO.StreamReader(fileName)
            Dim xdoc = XDocument.Load(New IO.StreamReader(fileName))
            If (xdoc.Root IsNot Nothing) Then
                With xdoc.Root
                    If (.@Version IsNot Nothing) Then
                        Me.Root.@Version = .@Version
                    End If
                    If .Element(XnsEdmx + "Runtime") IsNot Nothing Then Me.Root.Add(New Runtime(.Element(XnsEdmx + "Runtime")))
                    If .Element(XnsEdmx + "Designer") IsNot Nothing Then Me.Root.Add(New Designer(.Element(XnsEdmx + "Designer")))
                    If .Element(XnsEdmx + "DataServices") IsNot Nothing Then Me.Root.Add(New DataServices(.Element(XnsEdmx + "DataServices")))
                End With
            End If
        End Using

    End Sub

    <AutoLoad()> _
    <XmlElement("Runtime", GetType(Runtime))> _
    Public Property RunTime() As Runtime
        Get
            Return DirectCast(Me.Root.Element(XnsEdmx + "Runtime"), Runtime)
        End Get
        Set(ByVal value As Runtime)
            If (DirectCast(value, XElement).Name <> (XnsEdmx + "Runtime")) Then
                Throw New ArgumentException("Invalid Element")
            End If
            If (Me.RunTime IsNot Nothing) Then
                Me.RunTime.Remove()
            End If
            Me.Root.Add(New Runtime(value))
        End Set
    End Property

    <AutoLoad()> _
    <XmlElement("Designer", GetType(Designer))> _
    Public Property Designer() As Designer
        Get
            Return DirectCast(Me.Root.Element(XnsEdmx + "Designer"), Designer)
        End Get
        Set(ByVal value As Designer)
            If (DirectCast(value, XElement).Name <> (XnsEdmx + "Designer")) Then
                Throw New ArgumentException("Invalid Element")
            End If
            If (Me.Designer IsNot Nothing) Then
                Me.Designer.Remove()
            End If
            Me.Root.Add(New Designer(value))
        End Set
    End Property

    <AutoLoad()> _
    <XmlElement("DataServices", GetType(DataServices))> _
    Public Property DataServices() As DataServices
        Get
            Return DirectCast(Me.Root.Element(XnsEdmx + "DataServices"), DataServices)
        End Get
        Set(ByVal value As DataServices)
            If (DirectCast(value, XElement).Name <> (XnsEdmx + "DataServices")) Then
                Throw New ArgumentException("Invalid Element")
            End If
            If (Me.DataServices IsNot Nothing) Then
                Me.DataServices.Remove()
            End If
            Me.Root.Add(New DataServices(value))
        End Set
    End Property

    <XmlAttribute()> _
    Public Property Version() As String
        Get
            Return Me.Root.@Version
        End Get
        Set(ByVal value As String)
            Me.Root.@Version = value
        End Set
    End Property

End Class

